package j02;

import java.util.Date;

public class J1 {
    public static void main(String[] args) {
//        Thread tMain = Thread.currentThread();
//        System.out.println(tMain);
//
//        Thread t1 = new Thread(()->{
//            System.out.println(Thread.currentThread());
//        });
//        t1.start();
//        t1.setName("xiancheng-t1");
//        System.out.println("t1.name="+t1.getName());

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                System.out.println("t2-"+i);
            }
        },"t222222");
//        System.out.println("t2.getName() = " + t2.getName());
        t2.setPriority(1);
        Thread t3 = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                System.out.println("t3-"+i);
            }
        });
//        System.out.println("t2.getName() = " + t2.getName());
        t3.setPriority(10);
        t2.start();
        t3.start();
    }
}

class J2{
    static Thread t2;
    public static void main(String[] args) {

        Thread t1 = new Thread(()->{
            System.out.println("enter t1");
            try {
                t2.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 0; i < 100; i++) {
                System.out.println("t1="+i);
            }
        });
        t2 = new Thread(()->{
            System.out.println("enter t2");
            for (int i = 0; i < 100; i++) {
                System.out.println("t2="+i);
            }
        });
        t1.start();
        t2.start();
    }
}
class J3Sleep{
    public static void main(String[] args) throws InterruptedException {
        System.out.println("start main");
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        Thread.sleep(1000);
        while (true){
            Thread.sleep(1000);
            System.out.println(new Date());
        }
//        System.out.println("end main");

    }



}

class J4yeild{
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            System.out.println("enter t1");
            for (int i = 0; i < 100; i++) {
                System.out.println("t1="+i);
            }
        });
        Thread t2 = new Thread(()->{
            System.out.println("enter t2");
            for (int i = 0; i < 100; i++) {
                Thread.yield();//礼让
                System.out.println("t2="+i);
            }
        });
        t1.start();
        t2.start();
    }
}